package com.ruoyi.system.service.impl;

import com.ruoyi.common.constant.AgoniConstants;
import com.ruoyi.common.utils.JwtUtils;
import com.ruoyi.common.utils.MD5Utils;
import com.ruoyi.system.mapper.AgoniUserMapper;
import com.ruoyi.system.pojo.dto.UpdateUserDTO;
import com.ruoyi.system.pojo.dto.UserRegisterDTO;
import com.ruoyi.system.pojo.entity.User;
import com.ruoyi.system.pojo.vo.UserLoginVO;
import com.ruoyi.system.service.IAgoniUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
@Slf4j
public class IAgoniUserServiceImpl implements IAgoniUserService {

    @Autowired
    private AgoniUserMapper agoniUserMapper;

    /**
     * 登录用户
     */
    @Override
    public UserLoginVO login(String username, String password) {
        // TODO: 2021/11/25 登录用户
        //根据用户名查询用户信息
        User user = agoniUserMapper.selectByUsername(username);
        //判断用户是否存在
        if(user == null) {
            throw new RuntimeException("用户不存在");
        }
        //判断密码是否正确
        String salt = user.getSalt();
        String passwordMd5 = MD5Utils.enctype(password, salt, AgoniConstants.HASH_TIME);
        if(!passwordMd5.equals(user.getPassword())) {
            throw new RuntimeException("密码错误");
        }
        //登录成功
        UserLoginVO userLoginVO = new UserLoginVO();
        userLoginVO.setId(user.getId());
        userLoginVO.setUsername(user.getUsername());
        userLoginVO.setToken(JwtUtils.createToken(user.getUsername(), Long.valueOf(user.getId())));
        return userLoginVO;
    }

    /**
     * 注册用户
     * @param userRegisterDTO
     * @return
     */
    @Override
    public void register(UserRegisterDTO userRegisterDTO) {
        // TODO: 2021/11/25 注册用户
        //查看用户是否存在
        String username = userRegisterDTO.getUsername();

        if(agoniUserMapper.selectByUsername(username)!= null) {
            //存在则抛出异常
            throw new RuntimeException("用户名已存在");
        }

        //使用盐值+密码+散列次数进行加密
        String salt = UUID.randomUUID().toString().replace("-", "");
        String password = MD5Utils.enctype(userRegisterDTO.getPassword(), salt, AgoniConstants.HASH_TIME);
        //新建用户对象
        User user = new User();
        user.setPassword(password);
        user.setSalt(salt);
        user.setUsername(username);
        user.setEmail(userRegisterDTO.getEmail());
        //用户类型
        user.setUserType(AgoniConstants.USER_TYPE_NORMAL);
        //用户状态
        user.setStatus(AgoniConstants.USER_STATUS_NORMAL);

            //不存在则插入用户信息
            agoniUserMapper.insertUser(user);

        //不存在则插入用户信息
        return;
    }


    /**
     * TODO: web端用户管理模块
     */
    @Override
    public List<User> selectUserList() {
        return agoniUserMapper.selectUserList();
    }


    /**
     * App端获取用户信息
     * @param userId
     * @return
     */
    @Override
    public User getUserInfo(long userId) {
        return agoniUserMapper.selectByUserId(userId);
    }



    //Todo: 更新用户信息


    @Override
    public int updateUserInfo(UpdateUserDTO userDTO, long userId) {
        /**
         * 1. 根据userId查询用户信息
         * 2. 更新用户信息
         * 3. 返回更新结果
         * @return
         */
        User user = agoniUserMapper.selectByUserId(userId);
        if(user == null) {
            return 0;
        }
        user.setUsername(userDTO.getUsername());
        user.setEmail(userDTO.getEmail());
        user.setPhone(userDTO.getPhone());
        user.setAvatar(userDTO.getAvatar());

        int result = agoniUserMapper.updateUser(user);
        log.info("更新用户信息成功", result);


        return result;
    }
}
